package com.answerz.module_reportissues.fragment

import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Route
import com.answerz.module_reportissues.BR
import com.answerz.module_reportissues.R
import com.answerz.module_reportissues.adapter.ComplaintAdapter
import com.answerz.module_reportissues.databinding.FragmentComplaintListBinding
import com.answerz.module_reportissues.viewmodel.ComplaintListViewModel
import com.czl.base.base.BaseFragment
import com.czl.base.config.AppConstants
import com.czl.base.event.LiveBusCenter
import com.czl.base.util.DialogHelper

@Route(path = AppConstants.Router.Complaint.F_COMPLAINT_LIST)
class ComplaintListFragment : BaseFragment<FragmentComplaintListBinding, ComplaintListViewModel>() {

    private lateinit var mAdapter: ComplaintAdapter

    override fun initContentView(): Int {
        return R.layout.fragment_complaint_list
    }

    override fun initVariableId(): Int {
        return BR.viewModel
    }

    override fun initData() {
        if (arguments != null) {
            viewModel.ownerState =
                requireArguments().getString(AppConstants.BundleKey.OWNER_STATE).toString()
        }
        initAdapter()
        reload()
    }

    private fun initAdapter() {
        mAdapter = ComplaintAdapter(this)
        binding.ryCommon.apply {
            layoutManager = LinearLayoutManager(context, RecyclerView.VERTICAL, false)
            adapter = mAdapter
        }
    }

    override fun initViewObservable() {
        viewModel.uc.refreshListEvent.observe(this, {
            binding.smartCommon.autoRefresh()
        })
        viewModel.uc.loadCompleteEvent.observe(this, { data ->
            handleRecyclerviewData(
                data == null,
                data?.records as MutableList<*>?,
                mAdapter,
                binding.ryCommon,
                binding.smartCommon,
                viewModel.currentPage,
                data?.current == data?.pages
            )
        })
        viewModel.uc.cancelOrderEvent.observe(this, {
            DialogHelper.showBaseDialog(requireContext(), "", "确定撤销该投诉吗？") {
                viewModel.cancelComplaint(it)
            }
        })

        viewModel.uc.deleteOrderEvent.observe(this, {
            DialogHelper.showBaseDialog(requireContext(), "", "确定删除该投诉吗？") {
                viewModel.deleteComplaint(it)
            }
        })

        //跳转待评价页面
        viewModel.uc.navScoreEvent.observe(this, {
            startContainerActivity(AppConstants.Router.Report.F_SERVICE_SCORE,
                Bundle().apply {
                    putString(AppConstants.BundleKey.COMPLAINT_OREDER_NO, it)
                    putInt(AppConstants.BundleKey.COMMENT_TYPE_KEY,AppConstants.Constants.COMMENT_TYPE_COMPLAINT)
                })
        })
        viewModel.uc.navPayEvent.observe(this, {
            viewModel.getFree(it)
        })
        LiveBusCenter.observeCancelComplaintEvent(this, {
            reload()
        })
        LiveBusCenter.observeDeleteComplaintEvent(this, {
            reload()
        })

        LiveBusCenter.observePayResultEvent(this, {
            when (it.payType) {
                AppConstants.Constants.PAY_SUCCESS_TYPE -> {
                    reload()
                }
                AppConstants.Constants.PAY_FAIL_TYPE -> {
                    back()
                }
            }
        })

        //打分完之后之刷新已完成列表的数据
        LiveBusCenter.observeScoreCompleteEvent(this, {
            when (viewModel.ownerState) {
                "1" -> binding.smartCommon.autoRefresh()
            }
        })
    }

    override fun reload() {
        super.reload()
        binding.smartCommon.autoRefresh()
    }

    override fun useBaseLayout(): Boolean {
        return false
    }
}